package com.atguigu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;



public class LotterySystem extends JFrame implements ActionListener {
    private final JButton startButton;
    private final JButton pauseButton;
    private final JButton resetButton;
    private final JLabel resultLabel;
    private final JLabel inventoryLabel;
    private final JLabel titleLabel;
    private final JLabel statusLabel;
    private final JPanel lotteryPanel;
    private final Random random;
    private final Set<Integer> selectedNumbers;
    private boolean isRunning;
    private int inventoryCount;

    public LotterySystem() {
        super("保定市第十届大中专院校青年教师教学竞赛抽签系统");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(1400, 700);
        setLocationRelativeTo(null);
        getContentPane().setBackground(Color.gray);
        try {

            UIManager.put("RootPane.setupButtonVisible", false);
            UIManager.put("Button.background", Color.RED); // 设置按钮背景颜色为红色
            UIManager.put("Panel.background", Color.orange);  // 设置面板背景颜色为蓝色
        } catch (Exception e) {
            e.printStackTrace();
        }
        random = new Random();
        selectedNumbers = new HashSet<>();
        isRunning = false;
        inventoryCount = 22;

        titleLabel = new JLabel("保定市第十届大中专院校青年教师教学竞赛抽签系统", SwingConstants.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 48));

        statusLabel = new JLabel("尚未开始抽签", SwingConstants.CENTER);
        statusLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));

        resultLabel = new JLabel("", SwingConstants.CENTER);
        resultLabel.setFont(new Font("微软雅黑", Font.PLAIN, 24));
        resultLabel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100)); // 设置方框边框

        inventoryLabel = new JLabel("剩余号码数：" + inventoryCount, SwingConstants.CENTER);
        inventoryLabel.setFont(new Font("微软雅黑", Font.PLAIN, 36));

        startButton = new JButton("开始抽签");
        startButton.addActionListener(this);


        pauseButton = new JButton("暂停抽签");
        pauseButton.addActionListener(this);
        pauseButton.setEnabled(false);

        resetButton = new JButton("复位");
        resetButton.addActionListener(this);
        resetButton.setEnabled(false);
        Font buttonFont = new Font("微软雅黑", Font.BOLD, 24); // 创建字体对象，微软雅黑，粗体，大小16
        startButton.setFont(buttonFont); // 设置开始按钮字体样式
        pauseButton.setFont(buttonFont); // 设置暂停按钮字体样式
        resetButton.setFont(buttonFont); // 设置复位按钮字体样式
        startButton.setPreferredSize(new Dimension(200, 100)); // 设置按钮大小为宽100，高50
        pauseButton.setPreferredSize(new Dimension(200, 100)); // 设置按钮大小为宽100，高50
        resetButton.setPreferredSize(new Dimension(200, 100)); // 设置按钮大小为宽100，高50

        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(startButton);
        buttonPanel.add(pauseButton);
        buttonPanel.add(resetButton);

        lotteryPanel = new JPanel(new GridLayout(4, 1));
        lotteryPanel.add(titleLabel);
        lotteryPanel.add(statusLabel);
        lotteryPanel.add(resultLabel);
        lotteryPanel.add(inventoryLabel);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(lotteryPanel, BorderLayout.CENTER);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startButton) {
            startLottery();
        } else if (e.getSource() == pauseButton) {
            pauseLottery();
        } else if (e.getSource() == resetButton) {
            resetLottery();
        }
    }

    private void startLottery() {
        isRunning = true;
        statusLabel.setText("正在抽签中");
        startButton.setEnabled(false);
        pauseButton.setEnabled(true);
        resetButton.setEnabled(true);

        Thread lotteryThread = new Thread(() -> {
            while (isRunning) {
                int number = random.nextInt(22) + 1;
                if(number>=1&&number<10)  resultLabel.setText("抽中的号码是:0" + number);
                else resultLabel.setText("抽中的号码是:" + number);
                Font font = new Font("微软雅黑", Font.PLAIN, 56); // 示例中使用微软雅黑字体，普通样式，大小为20

// 将结果标签的字体设置为新的字体对象
                resultLabel.setFont(font);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        lotteryThread.start();
    }

    private void pauseLottery() {
        isRunning = false;
        pauseButton.setEnabled(false);
        int result = random.nextInt(22) + 1;
        while (selectedNumbers.contains(result)) {
            result = random.nextInt(22) + 1;
        }
        resultLabel.setText("抽中的号码是：" + result);
        selectedNumbers.add(result);
        inventoryCount--;
        inventoryLabel.setText("剩余号码数：" + inventoryCount);
        if (inventoryCount == 0) {
            statusLabel.setText("剩余号码数为0，抽签结束");
        } else {
            statusLabel.setText("已抽取一个号码，点击继续抽签");
            startButton.setEnabled(true);
        }
    }

    private void resetLottery() {
        isRunning = false;
        selectedNumbers.clear();
        inventoryCount = 22;
        inventoryLabel.setText("剩余号码数：" + inventoryCount);
        statusLabel.setText("尚未开始抽签");
        resultLabel.setText("");
        startButton.setEnabled(true);
        pauseButton.setEnabled(false);
        resetButton.setEnabled(false);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {

                    UIManager.put("RootPane.setupButtonVisible", false);
                    LotterySystem lotterySystem = new LotterySystem();
                    lotterySystem.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
